const express = require('express')
const path = require('path')
const fs = require('fs')
const { createBundleRenderer } = require('vue-server-renderer')

const serverBundle = path.resolve(process.cwd(), 'serverDist', 'vue-ssr-server-bundle.json')
const clientManifestPath = path.resolve(process.cwd(), 'dist', 'vue-ssr-client-manifest.json')
console.log(1, serverBundle)
console.log(clientManifestPath)
const clientManifest = require(clientManifestPath)

const template = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8')
const renderer = createBundleRenderer(serverBundle, {
    template,
    clientManifest
})

const app = express()
console.log(renderer)
app.use(express.static(path.resolve(process.cwd(), 'dist')))
app.get("*", function(req,res){
    renderer.renderToString({}, (err, html) => {
        if(err){
            console.log(err)
        }
        console.log(html)
        res.send(html)
    })
})

app.listen(3001)